package com.project;

import com.project.servlet.AddProduct;
import com.project.servlet.JSONServlet;
import com.project.servlet.TestServlet;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

/**
 * 启动类
 */
public class MainWeb1 {
    public MainWeb1(){
        Tomcat tomcat= new Tomcat();
        //设置tomcat启动端口
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //创建上下文
        Context context = tomcat.addContext("",null);
        try {
            //注册Servlet
            Wrapper w=Tomcat.addServlet(context,"testServlet",new TestServlet());
           //该值为整数或0时，表示容器一启动，完成该servlet的初始化动作
            //该值为负数或默认时，表示第一次访问该servlet完成初始化动作
            w.setLoadOnStartup(-1);
            //设置客户端请求的URL路劲
            w.addMapping("/wlh");

            Wrapper wJson=Tomcat.addServlet(context,"JSONServlet",new JSONServlet());
            wJson.addMapping("/json");

            Wrapper product = Tomcat.addServlet(context,"AddProduct",new AddProduct());

            product.addMapping("/product");
            //启动tomcat
            tomcat.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        new MainWeb1();
    }
}
